package tesis.alarma.demo1;

import com.sun.spot.resources.Resources;
import com.sun.spot.resources.transducers.IIOPin;
import com.sun.spot.resources.transducers.ISwitch;
import com.sun.spot.resources.transducers.IToneGenerator;
import com.sun.spot.resources.transducers.ITriColorLEDArray;
import com.sun.spot.resources.transducers.LEDColor;
import com.sun.spot.sensorboard.peripheral.ToneGenerator;
import com.sun.spot.service.BootloaderListenerService;
import com.sun.spot.util.Utils;

import java.io.IOException;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class DemoAlarma extends MIDlet {
    private ITriColorLEDArray leds = (ITriColorLEDArray) Resources.lookup(ITriColorLEDArray.class);
    private ISwitch sw1 = (ISwitch) Resources.lookup(ISwitch.class, "SW1");
    private ISwitch sw2 = (ISwitch) Resources.lookup(ISwitch.class, "SW2");
    /*variables para la alarma*/
    private boolean flag;
    private IToneGenerator speaker = (IToneGenerator) Resources.lookup(IToneGenerator.class, "speaker");
    private IIOPin pinDN = (IIOPin) Resources.lookup(IIOPin.class, "D0");

    private void ActivarAlarma(IToneGenerator speaker) {
        //Se crea un objetp speaker por donde saldra el tono de alarma
        //el flag es para que solo suene una vez, se los puedes quitar
        while ((sw2.isClosed()) && (flag)) {
            leds.getLED(5).setColor(LEDColor.TURQUOISE);
            leds.getLED(5).setOn();
            System.out.println("prueba ALARAMA de sonar");
            //tono en (frecuencoa[Hz],duracion[ms]
            speaker.startTone(2500, 300);
            Utils.sleep(600);
            speaker.startTone(2500, 300);
            Utils.sleep(600);
            speaker.startTone(2500, 300);
            Utils.sleep(600);
            //para resetear con todo con el boton sw1
            Desactivar();
        }

    }

    private void Desactivar() {
        if (sw1.isOpen()) {
            sw1.waitForChange();
        }
        flag = false;
        leds.getLED(5).setOff();

    }

    private void run() throws IOException {
        /*obajeto del tipo corneta por donde saldra el tono, en el pin*/
        speaker = new ToneGenerator(pinDN);
        for (int i = 0; i < leds.size(); i++) {
            leds.getLED(i).setOn();                    // Enable this LED
            leds.getLED(i).setRGB(0, 0, 0);              // Set it to black
        }
        leds.getLED(0).setColor(LEDColor.TURQUOISE);   // See LEDColor for more predefined colors.
        while (true) {
            flag = true;
            if (sw2.isOpen()) {
                sw2.waitForChange();
            }
           /*llamada a la funcion de alarma*/
            ActivarAlarma(speaker);
        }
    }

    /**
     * The rest is boiler plate code, for Java ME compliance
     *
     * startApp() is the MIDlet call that starts the application.
     */
    protected void startApp() throws MIDletStateChangeException {
        BootloaderListenerService.getInstance().start();       // Listen for downloads/commands over USB connection
        try {

            run();
        } catch (IOException ex) {              // A problem in reading the sensors. 
            ex.printStackTrace();
        }
    }

    /**
     * This will never be called by the Squawk VM.
     */
    protected void pauseApp() {
    }

    /**
     * Called if the MIDlet is terminated by the system. I.e. if startApp throws
     * any exception other than MIDletStateChangeException, if the isolate
     * running the MIDlet is killed with Isolate.exit(), or if VM.stopVM() is
     * called.
     *
     * It is not called if MIDlet.notifyDestroyed() was called.
     */
    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
    }
}
